<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script type="text/javascript">



//【示例1】下面代码是用JSON 编写的用户列表。
[{
    "id" : 1,
    "username" : "alice",
    "realname" : "Alice ",
    "email" : "alice@163.com"
}, {
    "id" : 2,
    "username" : "bob",
    "realname" : "Bob ",
    "email" : "bob@163.com"
}, {
    "id" : 3,
    "username" : "carol",
    "realname" : "Carol ",
    "email" : "carol@163.com"
}, {
    "id" : 4,
    "username" : "dave",
    "realname" : "Dave ",
    "email" : "dave@163.com"
}]

//【示例2】上面JSON数据也可以提炼成一个更简单的版本，将名字缩短：
[{
    "i" : 1,
    "u" : "alice",
    "r" : "Alice ",
    "e" : "alice@163.com"
}, {
    "i" : 2,
    "u" : "bob",
    "r" : "Bob ",
    "e" : "bob@163.com"
}, {
    "i" : 3,
    "u" : "carol",
    "r" : "Carol ",
    "e" : "carol@163.com"
}, {
    "i" : 4,
    "u" : "dave",
    "r" : "Dave ",
    "e" : "dave@163.com"
}]

//【示例3】也可以完全去掉属性名，与原格式相比，这种格式可读性更差，但更利索，文件尺寸非常小：大约只有标准JSON 格式的一半。
[
     [ 1, "alice", "Alice ", "alice@163.com" ],
     [ 2, "bob", "Bob", "bob@163.com" ],
     [ 3, "carol", "Carol ", "carol@163.com" ],
     [ 4, "dave", "Dave ", "dave@163.com" ]
]

//【示例4】在解析示例3的JSON数据时，需要保持数据的顺序，也就是说，这种精简格式在进行格式转换时必须保持和第一个JSON格式一样的属性名：
function parseJSON(responseText) {
    var users = [];
    var usersArray = ('(' + responseText + ')');
    for(var i = 0, len = usersArray.length; i < len; i++) {
        users[i] = {
            id : usersArray[i][0],
            username : usersArray[i][1],
            realname : usersArray[i][2],
            email : usersArray[i][3]
        };
    }
    return users;
}

//【示例5】下面示例代码使用JSON-P 格式编写用户列表。
parseJSON([{
    "id" : 1,
    "username" : "alice",
    "realname" : "Alice ",
    "email" : "alice@163.com"
}, {
    "id" : 2,
    "username" : "bob",
    "realname" : "Bob ",
    "email" : "bob@163.com"
}, {
    "id" : 3,
    "username" : "carol",
    "realname" : "Carol",
    "email" : "carol@163.com"
}, {
    "id" : 4,
    "username" : "dave",
    "realname" : "Dave ",
    "email" : "dave@163.com"
}]);



</script>
</head>
<body>

</body>
</html>
